package com.imoblife.brainwavestus.users;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.telephony.TelephonyManager;

public class UsersUtil {
	public static final String UPDATE_STATEVIEW_UI = "update_stateview_ui";
	public static final String UPDATE_STATEVIEW_USERCENTER = "update_stateview_usercenter";
	public static final String UPDATE_STATEVIEW_LOGIN = "update_stateview_login";
	public static final String UPDATE_LIBRERY_CATEGOTY = "update_librery_category";
	public static final String UPDATE_STATEVIEW_SUBSCRIPTION = "update_stateview_subscription";
	public static final String UPDATE_STATEVIEW_MYSUBSCRIPTION = "update_stateview_mysubscription";

	Context mContext;

	public UsersUtil(Context context) {
		this.mContext = context;
	}

	public boolean isConnectedNetwork() {
		ConnectivityManager cm = (ConnectivityManager) mContext
				.getSystemService(mContext.CONNECTIVITY_SERVICE);
		try {
			return cm.getActiveNetworkInfo().isConnected();
		} catch (Exception e) {
			return false;
		}
	}

	public boolean checkSupportGmail() {
		return getGmailAccount() != null;
	}

	public String[] getGmailAccount() {

		AccountManager accountManager = AccountManager.get(mContext);

		if (accountManager == null) {
			return null;
		}
		Account[] accounts = accountManager.getAccountsByType("com.google");
		if (accounts == null || accounts.length < 1) {
			return null;
		}
		String[] accountName = new String[accounts.length];
		for (int i = 0; i < accounts.length; i++) {
			accountName[i] = accounts[i].name;
		}
		return accountName;
	}

	public String getIMEI() {

		TelephonyManager telephonyManager = (TelephonyManager) mContext
				.getSystemService(Context.TELEPHONY_SERVICE);
		String IMEI = telephonyManager.getDeviceId();

		if (IMEI == null || IMEI.length() == 0
				|| IMEI.equals("000000000000000") || IMEI.equals("0")) {
			IMEI = "000000000000000";
		}

		return IMEI.toLowerCase();

	}

	public String getPoints(String price) {
		StringBuffer buffer = new StringBuffer(price);
		int size = buffer.length();
		for (int i = size - 3; i > 0; i -= 3) {
			buffer.insert(i, ", ");
		}
		return buffer.toString();
	}
	
	public void setIsAwardSuccess(Activity activity,boolean isSuccess) {
		SharedPreferences settings = activity.getSharedPreferences(
				"AwardTapjoyPoints", 0);
		settings.edit().putBoolean("isAwardPointsSuccess", isSuccess).commit();
	}

	public boolean getIsAwardSuccess(Activity activity) {
		SharedPreferences settings = activity.getSharedPreferences(
				"AwardTapjoyPoints", 0);
		return settings.getBoolean("isAwardPointsSuccess", true);
	}

	public void setIsGetMusicBillSuccess(Context context,boolean isSuccess) {
		SharedPreferences settings = context.getSharedPreferences(
				"GetMusicBill", 0);
		settings.edit().putBoolean("isGetMusicBillSuccess", isSuccess).commit();
	}

	public boolean getIsGetMusicBillSuccess(Activity activity) {
		SharedPreferences settings = activity.getSharedPreferences(
				"GetMusicBill", 0);
		return settings.getBoolean("isGetMusicBillSuccess", false);
	}
	
	public static String startTimeToEndTime(long tt, int offsetM) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
		java.util.Date dt = new Date(tt);
		dt.setMonth(dt.getMonth() + offsetM);
		return sdf.format(dt);
	}
	
	public static String formatTime(long tt) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
		return sdf.format(tt);
	}

}
